﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace Ama.BugTracker.Web.Handler
{
    /// <summary>
    /// AuthenticationHandler 的摘要说明
    /// </summary>
    public class AuthenticationHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //在此处写入您的处理程序实现。
            string name = context.Request.QueryString["name"];
            string pwd = context.Request.QueryString["pwd"];
            pwd = pwd == null ? string.Empty : pwd;

            context.Response.Write(ValidateUser(name, pwd));
        }

        private string ValidateUser(string name, string pwd)
        {
            DataRow row = UserSql.UserLoginCheck(name, pwd);
            if (!string.IsNullOrEmpty(row[0].ToString()))
            {                
                JsonSerializer json = new JsonSerializer();
                json.Converters.Add(new DataRowConverter());
                StringWriter sw = new StringWriter();
                JsonTextWriter writer = new JsonTextWriter(sw);

                writer.QuoteChar = '"';
                json.Serialize(writer, row);

                string output = sw.ToString();
                writer.Close();
                sw.Close();

                return output;                
            }
            else
                return string.Empty;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}